package heap;

import java.util.Vector;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


public class TestHeap {

	private Heap heap;
	
	@Before
	public void setUp() throws Exception {
		heap = new HeapImpl();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testIsEmpty() {
		Assert.assertTrue(heap.isEmpty());
		heap.insert(new HeapElement(6));
		Assert.assertFalse(heap.isEmpty());
		heap.extractMaximum();
		Assert.assertTrue(heap.isEmpty());
	}

	@Test
	public void testInsert() {
		for (int i = 0; i < 20; i++) {
			HeapElement he = new HeapElement(i);
			heap.insert(he);
			Assert.assertEquals(i, heap.maximum().getChave());
		}
	}

	@Test
	public void testExtractMaximum() {
		for (int i = 0; i < 20; i++) {
			HeapElement he = new HeapElement(i);
			heap.insert(he);
			Assert.assertEquals(new HeapElement(i), heap.maximum());
		}

	}

	@Test
	public void testMaximum() {
		for (int i = 0; i < 20; i++) {
			HeapElement he = new HeapElement(i);
			heap.insert(he);
			Assert.assertEquals(i, heap.maximum().getChave());
		}
	}

	@Test
	public void testHeapsort() {
		Vector<HeapElement> ordered = new Vector<HeapElement>();
		for (int i = 0; i < 25; i++) {
			HeapElement he = new HeapElement(i);
			heap.insert(he);
			ordered.addElement(he);
		}
		Assert.assertArrayEquals(ordered.toArray(new HeapElement[30]), heap.heapsort());
		
	}

}
